package com.tianji.learning.mq;

import com.tianji.common.constants.MqConstants;
import com.tianji.learning.enums.PointsRecordType;
import com.tianji.learning.service.IPointsRecordService;
import groovy.util.logging.Slf4j;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 消费消息,用于保存积分
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class LearningPointsListener {
	
	private static final Logger log = LoggerFactory.getLogger(LearningPointsListener.class);
	private final IPointsRecordService recordService;
	
	// 监听新增互动问答事件
	@RabbitListener(bindings = @QueueBinding(
			value = @Queue(name = "qa.points.queue", durable = "true"),
			exchange = @Exchange(name = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
			key = MqConstants.Key.WRITE_REPLY
	))
	public void listenWriteReplyMessage(Long userId) {
		recordService.addPointsRecord(userId, 5, PointsRecordType.QA);
	}
	
	// 监听签到事件
	@RabbitListener(bindings = @QueueBinding(
			value = @Queue(name = "sign.points.queue", durable = "true"),
			exchange = @Exchange(name = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
			key = MqConstants.Key.SIGN_IN
	))
	public void listenSignInMessage(SignInMessage message) {
		log.debug("签到增加的积分  消费到消息:      {}", message);
		recordService.addPointsRecord(message.getUserId(), message.getPoints(), PointsRecordType.SIGN);
	}
}